using Zhaoxi.Zhihu.SharedKerel.Messaging;
using Zhaoxi.Zhihu.SharedKerel.Result;
using Zhaoxi.Zhihu.UseCases.Common.Interface;

namespace Zhaoxi.Zhihu.UseCases.AppUsers.Queries;

public record LoginUserQuery(string Username, string Password) : IQuery<Result<TokenDto>>;

public class LoginUserQueryHandler(IIdentityService identityService):IQueryHandler<LoginUserQuery,Result<TokenDto>>
{
    public async Task<Result<TokenDto>> Handle(LoginUserQuery command, CancellationToken cancellationToken)
    {
       var result=await identityService.GetAccessTokenAsync(command.Username,command.Password);

       if (result.IsSuccess)
       {
           var token=new TokenDto(result.Value!);
           return Result.Success(token);
       }
       
       return Result.From(result);
    }
}

public record TokenDto
{
    public string AccessToken {get;set;}
    public TokenDto(string resultValue)
    {
        AccessToken = resultValue;
    }
}